package com.ssa.controller;

import com.jfoenix.controls.JFXListView;
import com.jfoenix.controls.JFXTabPane;
import com.sun.javafx.PlatformUtil;
import io.datafx.controller.ViewController;
import io.datafx.controller.flow.Flow;
import io.datafx.controller.flow.container.DefaultFlowContainer;
import io.datafx.controller.flow.context.FXMLViewFlowContext;
import io.datafx.controller.flow.context.ViewFlowContext;
import javafx.application.Platform;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Side;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.TextAlignment;

import javax.annotation.PostConstruct;

import static sun.plugin.javascript.navig.JSType.Image;


@ViewController(value = "/fxml/Main.fxml", title = "FX Client")
public class MainController {

    @FXMLViewFlowContext
    private ViewFlowContext context;
    @FXML
    private StackPane root;
    @FXML
    private JFXListView<StackPane> menuList;
    @FXML
    private JFXTabPane tabPane;

    private double tabWidth = 90.0;
    public static int lastSelectedTabIndex = 0;

    @PostConstruct
    public void init() throws Exception {
//        tabPane.setPrefSize(300, 200);
        Tab tab = new Tab();
        tab.setText("首页");

        Flow flow = new Flow(WebController.class);
        DefaultFlowContainer container = new DefaultFlowContainer();
//        context = new ViewFlowContext();
//        context.register("TabPane", tabPane);
        flow.createHandler(context).start(container);

        configureTab(tab, "首     页", "/icon/home.png");
        tab.setContent(container.getView());
        tabPane.getTabs().add(tab);

        tab = new Tab();
        configureTab(tab, "挂单挖矿", "/icon/order.png");
        tab.setContent(new Label("Content"));
        tabPane.getTabs().add(tab);

        tab = new Tab();
        configureTab(tab, "排序挖矿", "/icon/sort.png");
        tab.setContent(new Label("Content2"));
        tabPane.getTabs().add(tab);

        tab = new Tab();
        configureTab(tab, "交易挖矿", "/icon/trade.png");
        tab.setContent(new Label("Content3"));
        tabPane.getTabs().add(tab);

        tab = new Tab();
        configureTab(tab, "退     出", "/icon/logout.png");
        tab.setOnSelectionChanged(event -> {
            Tab currentTab = (Tab) event.getTarget();
            if (currentTab.isSelected()) {
                tabPane.getSelectionModel().select(lastSelectedTabIndex);
                Platform.exit();
            }
        });
        tabPane.getTabs().add(tab);


        tabPane.setSide(Side.LEFT);
        tabPane.setRotateGraphic(true);

        for (int i = 0; i < 4; i++) {
            Label label = new Label("东方公司的分公司的奉公守法的故事的风格东方公司的风格谁告诉对方告诉对方公司个");
            label.setWrapText(true);
            label.setMaxHeight(300);
            StackPane pane = new StackPane(root);
            pane.getChildren().add(label);
            menuList.getItems().add(pane);
        }
    }


    private void configureTab(Tab tab, String title, String iconPath) {
        double imageWidth = 40.0;

        /// 5.
        ImageView imageView = new ImageView(new Image(iconPath));
        imageView.setFitHeight(imageWidth);
        imageView.setFitWidth(imageWidth);

        Label label = new Label(title);
        label.setMaxWidth(tabWidth - 20);
        label.setPadding(new Insets(5, 0, 0, 0));
        label.setStyle("-fx-text-fill: white; -fx-font-size: 8pt; -fx-font-weight: normal;");
        label.setTextAlignment(TextAlignment.CENTER);

        BorderPane tabPane = new BorderPane();
        tabPane.setRotate(90.0);
        tabPane.setMaxWidth(tabWidth);
        tabPane.setCenter(imageView);
        tabPane.setBottom(label);

        /// 6.
        tab.setText("");
        tab.setGraphic(tabPane);
    }

}
